package com.wlyuan.index.remote.consumer;

import com.wlyuan.index.api.ICombineIndexService;
import com.wlyuan.index.api.IWaybillIndexService;
import com.wlyuan.index.domain.source.utils.SourceUtils;
import com.wlyuan.index.remote.message.QuotationMessage;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yuanjie
 */
@Slf4j
@Component
public class QuotationMessageConsumer extends AbstractMessageConsumer<QuotationMessage> {
    @Resource
    private IWaybillIndexService waybillIndexService;

    @Resource
    private ICombineIndexService combineIndexService;


    @RabbitListener(bindings = {@QueueBinding(value = @Queue(
            value = Queues.QUOTATION),
            exchange = @Exchange(value = Exchanges.QUOTATION),
            key = {
                    "QuotationCreated",
                    "QuotationUpdated",
                    "QuotationClosed",
                    "QuotationSubmitted",
                    "QuotationDiscarded",
                    "QuotationRecalled",
                    "QuotationAgreed",
                    "QuotationRejected",
                    "QuotationBargained",
                    "QuotationSurchargeAdded",
                    "QuotationRecalculated",
            })}, concurrency = "4")
    public void consume(String string) {
        if (logger.isDebugEnabled()) {
            logger.debug("收到报价消息: {}", string);
        }
        try {
            this.handle(string, QuotationMessage.class, message -> {
                var quotation = message.getQuotation();
                if (SourceUtils.isWaybillQuote(quotation)) {
                    waybillIndexService.index(quotation);
                } else if (SourceUtils.isCombineQuote(quotation)) {
                    combineIndexService.index(quotation);
                }
            });
        } catch (Exception e) {
            logger.error("处理报价消息失败：{}", string, e);
        }
    }
}
